<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/7/2 13:54
 * Modified Time: 2019/7/2 13:54
 * Version: 2.0
 */

namespace App\Modules\Inventory\Services;


use App\Exceptions\AllValidatorException;
use App\Lib\Auth\FLAuth;
use App\Lib\Utilities;
use App\Models\GoodsCategory;
use App\Models\Store;
use App\Modules\Inventory\Criterias\GoodsCategoryCriteria;
use App\Modules\Inventory\Repositories\GoodsCategoryRepository;
use App\Modules\Inventory\Repositories\GoodsRepository;
use Flower\Core\Repository\Eloquent\BaseService;
use Flower\Core\Repository\Exceptions\ValidatorException;
use Flower\Core\Support\Tree;
use Illuminate\Support\MessageBag;

class GoodsCategoryService extends BaseService
{

    protected $flAutu;
    protected $goodsRepository;
    protected $goodsCategoryRepository;

    public function __construct(GoodsCategoryRepository $goodsCategoryRepository,GoodsRepository $goodsRepository,FLAuth $flAuth)
    {
        $this->flAutu = $flAuth;
        $this->goodsCategoryRepository = $goodsCategoryRepository;
        $this->goodsRepository = $goodsRepository;
    }

    /*
     * 创建商品分类
     */
    public function createGoodsCategory($store_id,$parent_id,$name)
    {
        //判断父级分类id是否存在
        $this->checkParentId($store_id,$parent_id);

        return $this->goodsCategoryRepository->createGoodsCategory($store_id,$parent_id,$name);
    }

    /**
     * 编辑商品分类
     * @param $store_id
     * @param $category_id
     * @param $name
     * @throws ValidatorException
     */
    public function editGoodsCategory($store_id,$category_id,$name)
    {
        //判断分类是否存在
        $this->checkCategotryExist($store_id,$category_id);

        //更新数据
        return $this->goodsCategoryRepository->editGoodsCategory($category_id,$name);

    }

    /**
     * 删除店铺分类
     * @param $store_id
     * @param $category_id
     * @return int
     * @throws ValidatorException
     */
    public function delGoodsCategory($store_id,$category_id)
    {
        //判断分类是否存在
        $this->checkCategotryExist($store_id,$category_id);

        //判断分类是否能删
        $this->deletable($category_id);


        return $this->goodsCategoryRepository->delete($category_id);

    }

    /*
     * 获取本店所有分类
     */
    public function getGoodsCategoryList($store_id,$type,$parent_id)
    {
        $list = $this->goodsCategoryRepository->pushCriteria(new GoodsCategoryCriteria($store_id,$type,$parent_id))->all()->toArray();

        return $list;
    }

    /*
     * 判断分类是否存在
     */
    public function checkCategotryExist($store_id,$categotry_id)
    {
        $map = [
            ['id','=',$categotry_id],
            ['store_id','=',$store_id]
        ];
        $category = $this->goodsCategoryRepository->findByField($map)->first();
        if(!$category) {
            throw new AllValidatorException(['msg'=>'分类不存在']);
        }

    }



    /*
     * 判断一级父分类是否存在
     */
    protected function checkParentId($store_id,$parent_id)
    {
        if($parent_id!=0){
            $map = [
                ['id','=',$parent_id],
                ['store_id','=',$store_id],
                ['parent_id','=',0]
            ];
            $category = $this->goodsCategoryRepository->findByField($map)->first();
            if(!$category) {
                throw new ValidatorException(new MessageBag(['父级分类不存在']));
            }
        }

    }

    /*
     * 判断分类是否可删
     */
    protected function deletable($categotry_id){
        //获取分类实例
        $categotry = $this->goodsCategoryRepository->findByField('id',$categotry_id)->first();


        if($categotry->parent_id == 0){//一级分类
            //判断下面有无子分类
            $subset = $this->goodsCategoryRepository->findByField('parent_id',$categotry_id);
            if($subset->count()>0){
                throw new ValidatorException(new MessageBag(['不能删除有子分类的分类']));
            }
        } else { //二级分类
            //判断有无商品
            $goods = $this->goodsRepository->findByField('cat_id',$categotry_id);
            if($goods->count()>0){
                throw new ValidatorException(new MessageBag(['请先删除分类下的商品']));
            }
        }
    }

}